Page({

  /**
   * 页面的初始数据
   */
  data: {
    //题目信息 默认设置
    // qName,
    // qPhoto,
    // qContent,
    // qAward,
    // qId,
    // label: 0,
    qName: "用户01234",
    qPhoto: "/resources/Users/userphoto4.jpg",
    qContent: "关于操作系统，在生产着消费者流水线中，PV信号灯怎么设置？信号等数量怎么确定？1.dy/dx=(xy^2-cosxsinx)/(y(1-x^2)), y(0)=2 求y\n2.xydx+(2x^2+3y^2-20)dy=0, y(0)=1 求y\n3.dy/dx=(-2x+y)^2-7, y(0)=0 求y",
    qAward: 10,
    qId: 0,
    label: 0,
    conments: [],

    //用户操作
    nickName: "",
    avatarUrl: "",
    like_show: false,
    star_show: false,
    showInput: false,
    input_content: '',
    question: '',
    id: '',
    inputMarBot: false,
  },

  starthis: function (e) {
    console.log("~收藏");
    var flag = !this.data.star_show;
    console.log(this.data.star_show);
    this.setData({
      star_show: flag,
    })
  },
  likethis: function (e) {
    console.log("点赞");
    var flag = !this.data.like_show;
    this.setData({
      like_show: flag,
    })
  },
  commentthis: function (e) {
    console.log("评论");
    var flag = !this.data.showInput;
    this.setData({
      showInput: flag,
    })
  },

  setComment: function (e) {
    this.setData({
      input_content: e.detail.value
    })
    console.log(this.data.input_content)
  },

  send_comment: function (e) {
    console.log("this.qid:", this.data.qId);
    if (this.data.input_content.length == 0) {
      wx.showToast({
        title: '回复不能为空!',
        icon: 'loading',
        duration: 1500
      })
      // setTimeout(function () {
      //   wx.hideToast()
      // }, 2000)
    } else {
      const openid = wx.getStorageSync('openid');
      wx.request({
        url: 'http://localhost:8080/get_user_by_openid',
        method: "GET",
        data: {
          openid: openid
        },
        success: (res) => {
          console.log(res.data);
          // var tmp=res;
          wx.request({
            url: 'http://localhost:8080/comment',
            method: "POST",
            dataType: 'json',
            data: {
              user_id: openid,
              user_name: res.data.user_name,
              user_icon: res.data.user_icon,
              question_id: this.data.qId,
              content: this.data.input_content,
            },
            success: function (res) {
              console.log(res.data);
              if (res.data.status == 0) {
                wx.showToast({
                  title: '回复失败！！！',
                  icon: 'loading',
                  duration: 1500
                })
              } else {
                wx.showToast({
                  title: '回复成功！！！', //这里打印出成功
                  icon: 'success',
                  duration: 1000
                })
              }
            }
          })
        }
      })
    }
    var that = this;
    var comment = {
      usrName: this.data.nickName,
      usrPhoto: this.data.avatarUrl,
      desc: this.data.input_content,
      id: 123,
    };
    //向conment字段添加新的评论内容
    var list = that.data.conments;
    list.push(comment)
    // console.log(list)
    this.setData({
      conments: list
    })
    console.log(this.data.conments)
  },

  // 评论输入框聚焦时，设置与底部的距离
  settingMbShow: function () {
    this.setData({
      inputMarBot: true
    })
  },
  //  评论输入框失去聚焦时，设置与底部的距离（默认状态）
  settingMbNoShow: function () {
    this.setData({
      inputMarBot: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that=this;
    // var app = getApp();
    console.log(options.qid);
    var that = this;
    wx.request({
      url: 'http://localhost:8080/get_question_by_question_id',
      method: "GET",
      data: {
        question_id: options.qid
      },
      success: (res) => {
        console.log(res.data);
        // var that = this;
        this.setData({
          qContent: res.data.question_content,
          qAward: res.data.award,
          qId: res.data.question_id,
          label: res.data.label,
        })
        wx.request({
          url: 'http://localhost:8080/get_user_by_openid',
          method: "GET",
          data: {
            openid: res.data.user_id
          },
          success: (res) => {
            console.log(res.data);
            this.setData({
              qName: res.data.user_name,
              qPhoto: res.data.user_icon
            })
          }
        })
      }
    })
    wx.request({
      url: 'http://localhost:8080/get_comment_by_question',
      method: "GET",
      data: {
        question_id: options.qid
      },
      success: (res) => {
        console.log(res.data);
        console.log("hello");
        for (var i = 0, len = res.data.length; i < len; i++) {
          // var content=res.data[i].content;
          // console.log(content)
          var comment = {
            usrName: res.data[i].user_name,
            usrPhoto: res.data[i].user_icon,
            desc: res.data[i].content,
            qId: options.qid,
          }
          console.log("comment:", comment)
          console.log("comments:", that.data.conments)
          var list = that.data.conments;
          list.push(comment);
          this.setData({
            conments: list
          })
          console.log(this.data.conments)
          // this.i++;
        }
      }
    })
    var userInfo = wx.getStorageSync('userinfo');
    var that = this;
    that.setData({
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl
    })
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    let eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      // this.usrData=data;
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  }
})